package juc.c_020;/*
   Talk is cheap. Show me the code.
 */

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @Description
 * @Date 2021/6/8
 * @Author liudl
 */
public class T06_CountDownLatch {
    private static void countDownLatch(){
        Thread[] threads = new Thread[100];
        AtomicLong count = new AtomicLong(0);
        CountDownLatch latch = new CountDownLatch(threads.length);
        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(() -> {
                for (int i1 = 0; i1 < 10000; i1++) {
                    count.incrementAndGet();
                }
                System.out.println(Thread.currentThread().getName()+"："+ count);
                try {
                    TimeUnit.MICROSECONDS.sleep(60);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                latch.countDown();
            },"T-"+i);
        }

        for (Thread thread : threads) {
            thread.start();
        }
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(count);
        System.out.println("end......");
    }

    public static void main(String[] args) {
        T06_CountDownLatch.countDownLatch();
    }
}
